const fs = require('fs');
const path = require('path');

const baseDir = path.join(__dirname, 'mock');
const mockApi = function (res, pathname, params, next) {
	let data;
  switch (pathname) {
    case '/api/vote':
      data = fs.readFileSync(path.join(baseDir, 'vote.json'), 'utf-8');
      // res.setHeader('Content-Type', 'application/json');
      res.setHeader('Content-type', 'application/javascript');
      res.end(params.callback + '(' + data + ')');
      return;

    case '/api/getUserInfo':
      data = fs.readFileSync(path.join(baseDir, 'getUserInfo.json'), 'utf-8');
      res.setHeader('Content-type', 'application/javascript');
      res.end(params.callback + '(' + data + ')');
      return;
    case '/api/apply':
      data = fs.readFileSync(path.join(baseDir, 'apply.json'), 'utf-8');
      res.setHeader('Content-type', 'application/javascript');
      res.end(params.callback + '(' + data + ')');
      return;
    default:
  }
  next();
};

module.exports = mockApi;
